﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using A2DFramework.InstanceService;
using A2DFramework.QueueService.Providers;

namespace A2DFramework.QueueService.QueueConfiguration
{
    public static class QueueConfiguration
    {
        public static MSMQSettings MSMQSettings;
        public static RabbitMQSettings RabbitMQSettings;
        public static RedisSettings RedisSettings;
        public static QueueType QueueType;

        static QueueConfiguration()
        {
            XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config"));

            root = root.Element("QueueSetting");
            if (root == null)
                throw new Exception("QueueSetting node not exists");

            if (root.Attribute("QueueType").Value.Equals("MSMQ", StringComparison.OrdinalIgnoreCase))
            {
                root = root.Element("MSMQ");

                QueueType = A2DFramework.QueueService.QueueType.MSMQ;

                MSMQSettings setting = new MSMQSettings();

                setting.QueueFormat = root.Element("QueueConnectionStringFormat").Value;
                setting.NetworkLocation = root.Element("NetworkLocation").Value;

                if (root.Element("AutoDeleteNetworkFile") != null
                    && root.Element("AutoDeleteNetworkFile").Value != null
                    && root.Element("AutoDeleteNetworkFile").Value.Trim().Length > 0)
                    setting.AutoDeleteNetworkFile = bool.Parse(root.Element("AutoDeleteNetworkFile").Value);
                else
                    setting.AutoDeleteNetworkFile = true;//default auto delete

                if (root.Element("MaxQueueBodyLength") != null
                    && root.Element("MaxQueueBodyLength").Value != null
                    && root.Element("MaxQueueBodyLength").Value.Trim().Length > 0)
                    setting.MaxQueueBodyLength = double.Parse(root.Element("MaxQueueBodyLength").Value);
                else
                    setting.MaxQueueBodyLength = 3.5 * 1024 * 1024;

                MSMQSettings = setting;
            }
            else if (root.Attribute("QueueType").Value.Equals("Redis", StringComparison.OrdinalIgnoreCase))
            {
                root = root.Element("Redis");

                QueueType = A2DFramework.QueueService.QueueType.Redis;

                RedisSettings setting = new RedisSettings();

                setting.ReadWriteServerIps = new List<string>();
                setting.ReadOnlyServerIps = new List<string>();

                {
                    string[] serverIps=root.Element("ReadWriteServerIps").Value.Trim().Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (serverIps != null)
                        setting.ReadWriteServerIps = serverIps.ToList();
                }
                {
                    string[] serverIps = root.Element("ReadOnlyServerIps").Value.Trim().Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (serverIps != null)
                        setting.ReadOnlyServerIps = serverIps.ToList();
                }

                setting.MaxReadPoolSize = int.Parse(root.Element("MaxReadPoolSize").Value);
                setting.MaxWritePoolSize = int.Parse(root.Element("MaxWritePoolSize").Value);
                setting.QueueNameFormat = root.Element("QueueNameFormat").Value;

                RedisSettings = setting;
            }
            else if (root.Attribute("QueueType").Value.Equals("RabbitMQ", StringComparison.OrdinalIgnoreCase))
            {
                root = root.Element("RabbitMQ");

                QueueType = A2DFramework.QueueService.QueueType.RabbitMQ;

                RabbitMQSettings setting = new RabbitMQSettings();

                setting.Server = root.Element("Server").Value;
                setting.Port = int.Parse(root.Element("Port").Value);
                setting.User = root.Element("User").Value;
                setting.Password = root.Element("Password").Value;
                setting.QueueNameFormat = root.Element("QueueNameFormat").Value;

                RabbitMQSettings = setting;
            }
            else
            {
                throw new Exception("QueueSetting QueueType属性值必须为MSMQ或者Redis或者RabbitMQ");
            }
        }
    }
}
